function News() {
  return this.init()
}
News.prototype = {
  url: 'https://h-college.737.com/api/hqxy/home/',
  API: {
    GetNewsInit: 'newsInit',
    GetNewsList: 'newsList',
    GetNewsInfo: 'newsInfo/',
    GetNewBanner: 'carousel',
    GetWallpaper: 'wallpaper',
  },
  newsType: {
    1: '新闻',
    2: '公告',
    3: '活动'
  },
  init: function () {
    // console.log('news init');
    return this
  },

  // 拉取首页新闻列表
  getNewsLists: function (cb) {
    this.httpRequest('GET', this.url + this.API.GetNewsInit, null, function (result) {
      if (result && result.errCode === 0) {
        // 渲染新闻列表
        cb(result)
      } else {
        // console.log(JSON.stringify(result));
      }
    })
  },

  // 拉取新闻列表
  getNewsList: function (data, cb) {
    this.httpRequest('GET', this.url + this.API.GetNewsList + `?page=${data.page}&pageSize=${data.pageSize}&type=${data.type}`, null, function (result) {
      if (result && result.errCode === 0) {
        cb(result)
      } else {
        // console.log(JSON.stringify(result));
      }
    })
  },

  // 拉取bannner新闻
  getBannerNews: function(cb) {
    this.httpRequest('GET', this.url + this.API.GetNewBanner, null, function (result) {
      if (result && result.errCode === 0) {
        cb(result)
      } else {
        // console.log(JSON.stringify(result));
      }
    })
  },

  getNewsInfo: function(data, cb) {
    this.httpRequest('GET', this.url + this.API.GetNewsInfo + data.nid, null, function (result) {
      if (result && result.errCode === 0) {
        cb(result)
      } else {
        // console.log(JSON.stringify(result));
      }
    })
  },

  getWallpaper: function (data, cb) {
    this.httpRequest('GET', this.url + this.API.GetWallpaper + `?type=${data.type}&start=${data.start}&end=${data.end}`, null, function (result) {
      if (result && result.errCode === 0) {
        cb(result)
      } else {
        // console.log(JSON.stringify(result));
      }
    })
  },

  // 时间格式化
  timeFormat: function (time) {
    let date = new Date(time)
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    return `${year}/${month}/${day}`;
  },

  // ajax 封装
  httpRequest: function (type, url, data, cb) {
    $.ajax({
      type: type,
      contentType: "application/json;charset=UTF-8",
      url: url,
      data: data ? JSON.stringify(data) : null,
      success: function (result) {
        cb(result);
      },
      error: function (e) {
        let data = {
          errCode: -1,
          data: {
            status: e.status,
            errMsg: e.responseText
          }
        }
        cb(data)
      }
    });
  }
}